﻿/// <reference path="jquery-1.8.1-vsdoc.js" />

/// <reference path="cui-core.js" />

//////////////////////////////////////////////////////////////////////////
/// CMAT page Common
//////////////////////////////////////////////////////////////////////////
var global = {
    CMATPages: { searchPage: null },
    sidebarRight: null,
    sidebarTop: null
};
global.CMATPages = {
    searchPage: null
};

function InitiallGlobal() {
    global.sidebarRight = $("#rightsider").sidebar();
}

//Define the base CMAT Page
function CMATPage() {
    //Inherit the CUIpage
    CUIPage.call(this);
    //initial the options
    this.option = null;
}

CMATPage.prototype = new CUIPage();
CMATPage.prototype.GetPageData = function () {

};

CMATPage.prototype.DataManager = function () {

};

var EventManager = function () {

    if (typeof CMATPage.prototype.EventManager.setDefaultBaseEvents !== "function") {
        CUICore.getCurrentPage = function () {
            return $("body[searchPage]").attr("searchPage");
        };
        CUICore.baseControlOption.attribute.OnValidateError = "DefalueValidateError";
        CUICore.baseControlOption.attribute.OnValidatePass = "DefalueValidatePass";
    }
}


//Define base page object
function CMATPage() {
    var self = this;

    //Inherit the CUIpage
    CUIPage.call(this);

    //initial the options
    this.option = null;

    //initial submit event
    this.pageEvent["submit"] = function () {
        var x = $("#seachCriteria").submitValidate();
        if (x !== null) {
            var temp = "";
            $.each(x, function (key, value) {
                temp += String.stringFormat("[{0} : {1}] ", key, value);
            });
            $("#inputText").text(temp);
            global.sidebarRight.show();
        }
    };
    //initial initial page render
    this.pageEvent["SetNextStep"] = function ($element) {
        var self = $element;
        var uiElement = self.closest(".cui");
        var currentPageName = $element.closest("[pageName]").attr("pageName");
        var itemList = null;
        $(global.CMATPages[currentPageName].option).each(function (index, temp) {
            itemList = temp[self.getValue()];
        });
        if (itemList) {
            uiElement.nextAll().remove();

            $(itemList).each(function (index, item) {
                if (item.kind) {
                    var element = null;
                    item.renderType = CUICore.renderType.after;
                    if (item.hasNext) {
                        item.attribute.onChanged = "SetNextStep";
                    }
                    //In CMAT most of control is disableCopy: true
                    item.disableCopy = true;
                    switch (item.kind) {
                        case CUICore.pluginType.cTextbox:
                            element = uiElement.cTextbox(item);
                            break;
                        case CUICore.pluginType.cDropdownList:
                            element = uiElement.cDropdownList(item);
                            break;
                        case CUICore.pluginType.cRadioGroup:
                            element = uiElement.cRadioGroup(item);
                            break;
                        case CUICore.pluginType.cCheckbox:
                            element = uiElement.cCheckbox(item);
                            break;
                        case CUICore.pluginType.cTextarea:
                            element = uiElement.cTextarea(item);
                            break;
                        default:
                            return;
                    }
                }
                if (item.hasNext) {
                    if (item.tip == undefined) {
                        element.change();
                    }
                }
            });
        }
        else {
            uiElement.nextAll().remove();
        }
    };
    this.initialPageByOption = function () {
        return null;
    }
}
CMATPage.prototype = new CUIPage();

$(document).ready(function () {
    //initial the global
    InitiallGlobal();
    //inherit the CMATPage
    global.CMATPages.searchPage = new CMATPage();
    global.CMATPages.searchPage.submit = $("#submit").cButton({
        tip: "submit",
        attribute: {
            id: "btnDemo",
            onItemClick: "submit"
        }
    });

    $.post("GetOption", null, function (option) {
        global.CMATPages.searchPage.option = option;
        global.CMATPages.searchPage.initialPageByOption = function () {
            var self = this;
            if (self.option !== null) {
                if ($("#seachCriteria")) {
                    $("#seachCriteria").cDropdownList({
                        tip: "Agent type",
                        attribute: { id: "agentType", required: true, onChanged: "SetNextStep" },
                        data: { o365: "o365", azure: "azure", xbox: "xbox" }
                    });
                }
            }
            else {
                if (CUICore.debug) {
                    alert("The option is incorrect or null!");
                }
            }
        };
        global.CMATPages.searchPage.initialPageByOption(option);
        global.CMATPages.searchPage.InitialPage();
    });

});

//The options config object for search page
//var option = [{
//    //--------level1
//    o365: [{
//        kind: CUICore.pluginType.cDropdownList,
//        data: { o365_tenant_guid: "Tenant Guid", o365_ou_guid: "OU Guid", subscription_id: "Subscription Id", subscription_guid: "Subscription Guid", orgid_email: "Orgid Email", order_number: "Order Number", invoice_number: "Invoice Number" },
//        hasNext: true,
//        attribute: { id: "search_type" }
//    }],
//    azure: [{
//        kind: CUICore.pluginType.cDropdownList,
//        data: { azure_live_id: "Live ID", azure_customer_information: "Customer Information", subscription_id: "Subscription Id", subscription_guid: "Subscription Guid", orgid_email: "Orgid Email", order_number: "Order Number", invoice_number: "Invoice Number" },
//        hasNext: true,
//        attribute: { id: "search_type", required: true, hasSubpanel: true }
//    }],
//    xbox: [{
//        kind: CUICore.pluginType.cDropdownList,
//        data: { customer_information: "Customer Info", id: "id", payment_instrument: "Payment Instrument", merchant_id: "Merchant Id" },
//        hasNext: true,
//        attribute: { id: "search_type" }
//    }],
//    //----------------level2
//    o365_tenant_guid: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Tenant Guid",
//        attribute: { id: "tenant_guid", required: true, maxlength: 64 }
//    }],
//    o365_ou_guid: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "OU Guid",
//        attribute: { id: "ou_guid", required: true, maxlength: 64 }
//    }],
//    subscription_id: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Subscription Id",
//        attribute: { id: "subscription_id", required: true, maxlength: 64 }
//    }],
//    subscription_guid: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Subscription Guid",
//        attribute: { id: "subscription_guid", required: true, maxlength: 64 }
//    }],
//    orgid_email: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Orgid Email",
//        attribute: { id: "orgid_email", validateType: CUICore.validateType.email, required: true, maxlength: 64 }
//    }],
//    order_number: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Order Number",
//        attribute: { id: "order_number", required: true, maxlength: 64 }
//    }],
//    invoice_number: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Invoice Number",
//        attribute: { id: "invoice_number", required: true, maxlength: 64 }
//    }],
//    azure_live_id: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Live ID",
//        attribute: { id: "live_id", validateType: CUICore.validateType.email, required: true, maxlength: 64 }
//    }],
//    azure_customer_information: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "First Name",
//        attribute: { id: "first_name", required: true, maxlength: 64 }
//    }, {
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Last Name",
//        attribute: { id: "last_name", required: true, maxlength: 64 }
//    }],
//    invoice_number: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Invoice Number",
//        attribute: { id: "invoice_number", required: true, maxlength: 64 }
//    }],
//    customer_information: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "First Name",
//        attribute: { id: "first_name", required: true, maxlength: 64 }
//    }, {
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Last Name",
//        attribute: { id: "last_name", required: true, maxlength: 64 }
//    }],
//    id: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Live Id",
//        attribute: { id: "id_account_id", required: true, validateType: CUICore.validateType.email, maxlength: 64 }
//    },
//        {
//            kind: CUICore.pluginType.cTextbox,
//            tip: "Account Id",
//            attribute: { id: "account_id", required: true, maxlength: 64 }
//        }],
//    payment_instrument: [{
//        kind: CUICore.pluginType.cDropdownList,
//        data: { global: "Global", credit_card: "Credit Card", direct_debit: "Direct Debit", qwest_account: "Qwest Account", invoice: "Invoice", wholesale: "Wholesale", paypal: "Paypal" },
//        hasNext: true,
//        attribute: { id: "payment_instrument", required: true }
//    }],
//    merchant_id: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Merchant Id",
//        attribute: { id: "merchant_id", required: true, maxlength: 64 }
//    }],
//    //------------------------level3
//    global: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Global",
//        attribute: { id: "merchant_id", required: true, maxlength: 64 }

//    }],
//    credit_card: [{
//        kind: CUICore.pluginType.cDropdownList,
//        data: { visa: "Visa", mastercard: "Master Card", american_express: "American Express", discover: "Discover", jcb: "JCB" },
//        hasNext: true,
//        attribute: { id: "credit_card", required: true }
//    }],
//    direct_debit: [{
//        kind: CUICore.pluginType.cDropdownList,
//        data: { US: "United States", GB: "United Kingdom", FR: "France", DE: "Germany" },
//        hasNext: true,
//        attribute: { id: "direct_debit", required: true }
//    }],
//    qwest_account: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Phone Number",
//        disableCopy: true,
//        attribute: { id: "qa_phone_number", validateType: CUICore.validateType.number, required: true, maxlength: 12 }
//    }],
//    invoice: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Attention Of",
//        disableCopy: true,
//        attribute: { id: "attention_of", required: true, maxlength: 64 }
//    }, {
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Company PO Number",
//        disableCopy: true,
//        attribute: { id: "company_po_number", required: true, maxlength: 30 }
//    }, {
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Sap Customer Number",
//        disableCopy: true,
//        attribute: { id: "sap_customer_number", validateType: CUICore.validateType.number, required: true, maxlength: 10 }
//    }],
//    wholesale: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "External Partner Id",
//        disableCopy: true,
//        attribute: { id: "external_partner_id", required: true, maxlength: 64 }
//    }],
//    paypal: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Billingagreement Id",
//        disableCopy: true,
//        attribute: { id: "billingagreement_id", required: true, maxlength: 64 }
//    }],
//    //--------------------------------level4
//    visa: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Visa",
//        attribute: { id: "visa_account_number", validateType: CUICore.validateType.number, required: true, maxlength: 16 }
//    }],
//    mastercard: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Master Card",
//        disableCopy: true,
//        attribute: { id: "mastercard_account_number", validateType: CUICore.validateType.number, required: true, maxlength: 16 }
//    }],
//    american_express: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "American Express",
//        attribute: { id: "american_express_account_number", validateType: CUICore.validateType.number, required: true, maxlength: 16 }
//    }],
//    discover: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Discover",
//        attribute: { id: "discover_account_number", validateType: CUICore.validateType.number, required: true, maxlength: 16 }
//    }],
//    jcb: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "JCB",
//        attribute: { id: "jcb_account_number", validateType: CUICore.validateType.number, required: true, maxlength: 16 }
//    }],
//    US: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Account Number",
//        attribute: { id: "db_account_number", required: true, maxlength: 17, minlength: 4 }
//    }, {
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Bank Code",
//        disableCopy: true,
//        attribute: { id: "db_bank_code", required: true, maxlength: 9, minlength: 9 }
//    }, {
//        kind: CUICore.pluginType.cDropdownList,
//        data: { saving: "Saving", checking: "Checking" },
//        attribute: { id: "db_type", required: true }
//    }],
//    GB: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Account Number",
//        attribute: { id: "db_account_number", required: true, maxlength: 12, minlength: 6 }
//    }, {
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Branch Code",
//        attribute: { id: "db_branch_code", required: true, maxlength: 9, minlength: 6 }
//    }],
//    FR: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Bank Code",
//        attribute: { id: "db_account_number", required: true, maxlength: 9, minlength: 6 }
//    }, {
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Account Number",
//        attribute: { id: "db_bank_code", required: true, maxlength: 5, minlength: 5 }
//    }, {
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Branch Code",
//        attribute: { id: "db_branch_code", required: true, maxlength: 5, minlength: 5 }
//    }],
//    DE: [{
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Bank Code",
//        attribute: { id: "db_account_number", required: true, maxlength: 8, minlength: 8 }
//    }, {
//        kind: CUICore.pluginType.cTextbox,
//        tip: "Branch Code",
//        attribute: { id: "db_branch_code", required: true, maxlength: 10, minlength: 2 }
//    }]
//}];